package server.controller;

import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;

public class PcAutoUpdateController extends TimerTask {
	private static Logger _log = Logger
			.getLogger(CharacterAutoSaveController.class.getName());
	private static Timer _characterAutoUpdateTimer = new Timer();
	private final int _updateTime;

	public PcAutoUpdateController(int updateTime) {
		_updateTime = updateTime;
	}

	public void start() {
		_characterAutoUpdateTimer.scheduleAtFixedRate(
				PcAutoUpdateController.this, 0, _updateTime);
	}

	@Override
	public void run() {
		try {
			for (L1PcInstance _client : L1World.getInstance().getAllPlayers()) {
				if (_client == null || _client.getNetConnection() == null) {
					continue;
				} else {
					try {
						_client.updateObject();
					} catch (Exception e) {
						_log.warning("Client autoupdate failure.");
						_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
						throw e;
					}
				}
			}
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
			// cancel();
		}
	}
}
